﻿$import( "SmitearWebGL.Core.ResourceItem" );
$import( "SmitearWebGL.Core.Geometry.InputDataSet" );

SmitearWebGL.Objects.ShadowMesh = SmitearWebGL.Core.ResourceItem.extend(
{
	/******** constructor ********/
	
	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Objects.ShadowMesh" );
	}
	,
	createVertexBuffer : function( bufferData )
	{
		this._vertexBuffer = this.context.resourceManager.createResource( "__vb_" + this.name, SmitearWebGL.Resources.VertexBuffer );
		this._vertexBuffer.bindData( bufferData );
		this._vertexBuffer.createElementDescription( 3, 24, 0, SmitearWebGL.Resources.Buffer.DataType.FLOAT, SmitearWebGL.Resources.Buffer.AttribLocation.POSITION );
		this._vertexBuffer.createElementDescription( 3, 24, 12, SmitearWebGL.Resources.Buffer.DataType.FLOAT, SmitearWebGL.Resources.Buffer.AttribLocation.NORMAL );
	}
	,
	createIndexBuffer : function( bufferData )
	{
		this._indexBuffer = this.context.resourceManager.createResource( "__ib_" + this.name, SmitearWebGL.Resources.IndexBuffer );
		this._indexBuffer.bindData( bufferData );
		this._indexBuffer.createElementDescription( bufferData.length, 0, SmitearWebGL.Resources.Buffer.DataType.UNSIGNED_SHORT, SmitearWebGL.Resources.IndexBuffer.DrawType.TRIANGLES );
	}
	,
	renderCallBack : function()
	{
		var program = this.context.programManager.getResourceByName( SmitearWebGL.Programs.InternalPrograms.Type.SHADOW_VOLUME );
		
		var hr = SmitearWebGL.Core.HResult.SUCCESS;
		
		hr |= program.activeUniform( this._shelter );
		
		hr |= this._vertexBuffer.bind();
		hr |= this._vertexBuffer.active( program.getShader() );
		
		hr |= this._indexBuffer.bind();
		hr |= this._indexBuffer.draw( this._shelter._material.renderMode );
		
		this.debugRenderStatus( hr );
		
		return hr;
	}
	,
	debugRenderStatus : function( hResult )
	{
		if ( hResult > 0 )
			$WARNING( "警告:" + this._shelter.name + "出现渲染状态异常,代码:" + hResult );
	}
	,
	_createResource : function( params )
	{
		this._super();
		
		this._vertexBuffer = null;
		this._indexBuffer = null;
		this._shelter = params.shelter;
		
		var vOutputData = SmitearWebGL.Core.Geometry.Geometry.createShadowMesh( new SmitearWebGL.Core.Geometry.InputDataSet( this._shelter.getIndices(), this._shelter.getVertices(), 3 ) );
		
		this.createVertexBuffer( vOutputData.data );
		this.createIndexBuffer( vOutputData.indices );
	}
	,
	_destroyResource : function()
	{
		this._super();
		
		this._vertexBuffer.release();
		this._indexBuffer.release();
	}
}
);